/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>  
#include <string.h>
#include <stdint.h>

#define HASH_SIZE 32
#define ITERATIONS 64

void custom_hash(const uint8_t *input, size_t input_len, uint8_t *output) {  
    uint32_t hash[8] = {0};
    uint32_t temp;

    for (size_t i = 0; i < 8; i++) {  
        hash[i] = i * 2654435761U;
    }  


    for (size_t i = 0; i < input_len; i++) {
        for (int j = 0; j < 8; j++) {  
            temp = (hash[j] + input[i]) ^ ((hash[(j + 1) % 8] >> 3) | (hash[(j + 1) % 8] << (32 - 3)));  
            hash[j] = temp * 0x01000193;
        }  
    }  

    for (int iter = 0; iter < ITERATIONS; iter++) {  
        for (int j = 0; j < 8; j++) {  
            temp = (hash[j] + (iter ^ 0x5A5A5A5A)) * 0x5bd1e995;
            hash[j] = (temp ^ (hash[(j + 1) % 8] << 7)) ^ (hash[(j + 2) % 8] >> 11);  
        }  
    }  

    for (int i = 0; i < HASH_SIZE; i++) {  
        output[i] = (hash[i % 8] >> (i % 32)) & 0xFF;
    }  
}  

void print_hex(const uint8_t *data, size_t length) {  
    for (size_t i = 0; i < length; i++) {  
        printf("%02x", data[i]);  
    }  
    printf("\n");  
}  

int main() {  
    const char *input_string = "Hello, Custom Hash!";
    uint8_t hash[HASH_SIZE];

    custom_hash((const uint8_t *)input_string, strlen(input_string), hash);  

    printf("Custom Hash: ");  
    print_hex(hash, HASH_SIZE);  

    return 0;  
}
